package cn.iocoder.yudao.module.call.controller.admin.timbre;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.timbre.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.timbre.TimbreDO;
import cn.iocoder.yudao.module.call.service.timbre.TimbreService;

@Tag(name = "管理后台 - ivr-音色")
@RestController
@RequestMapping("/call/timbre")
@Validated
public class TimbreController {

    @Resource
    private TimbreService timbreService;

    @PostMapping("/create")
    @Operation(summary = "创建ivr-音色")
    @PreAuthorize("@ss.hasPermission('call:timbre:create')")
    public CommonResult<Long> createTimbre(@Valid @RequestBody TimbreSaveReqVO createReqVO) {
        return success(timbreService.createTimbre(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ivr-音色")
    @PreAuthorize("@ss.hasPermission('call:timbre:update')")
    public CommonResult<Boolean> updateTimbre(@Valid @RequestBody TimbreSaveReqVO updateReqVO) {
        timbreService.updateTimbre(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ivr-音色")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:timbre:delete')")
    public CommonResult<Boolean> deleteTimbre(@RequestParam("id") Long id) {
        timbreService.deleteTimbre(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ivr-音色")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:timbre:query')")
    public CommonResult<TimbreRespVO> getTimbre(@RequestParam("id") Long id) {
        TimbreDO timbre = timbreService.getTimbre(id);
        return success(BeanUtils.toBean(timbre, TimbreRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ivr-音色分页")
    @PreAuthorize("@ss.hasPermission('call:timbre:query')")
    public CommonResult<PageResult<TimbreRespVO>> getTimbrePage(@Valid TimbrePageReqVO pageReqVO) {
        PageResult<TimbreDO> pageResult = timbreService.getTimbrePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TimbreRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得ivr-音色列表")
    public CommonResult<List<TimbreRespVO>> getTimbreSimpleList() {
        TimbrePageReqVO pageReqVO = new TimbrePageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<TimbreDO> pageResult = timbreService.getTimbrePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult.getList(), TimbreRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ivr-音色 Excel")
    @PreAuthorize("@ss.hasPermission('call:timbre:export')")
    @OperateLog(type = EXPORT)
    public void exportTimbreExcel(@Valid TimbrePageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TimbreDO> list = timbreService.getTimbrePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ivr-音色.xls", "数据", TimbreRespVO.class,
                BeanUtils.toBean(list, TimbreRespVO.class));
    }

}